package com.changpay.zywy.ui.activity.count;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.changpay.zywy.R;
import com.changpay.zywy.adapter.MonthTrandAdapter;
import com.changpay.zywy.bean.MerchantTrading;
import com.changpay.zywy.ui.dialog.SelectMonthDialog;
import com.changpay.zywy.utils.DateUtil;
import com.ruffian.library.widget.RTextView;
import kale.ui.view.dialog.EasyDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AndroidIntexh1 on 2018/12/20.
 */

public class CountTrandShopView extends RelativeLayout {

    RelativeLayout rvMore;
    TextView tvTitle;
    TextView tv_text;
    LinearLayout llContent;
    RTextView tvTimeSearch;
    RecyclerView recyclerView;

    private MonthTrandAdapter mAdapter;
    private List<MerchantTrading.MerchantTradingBean> datas = new ArrayList<>();



    CountChartActionClose countChartOtherClose;


    private View view;

    public void setFragmentManager(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
    }

    FragmentManager fragmentManager;
    private String month;


    public CountTrandShopView(Context context) {
        this(context, null);
    }

    public CountTrandShopView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        month = DateUtil.INSTANCE.getYearMonth(System.currentTimeMillis());
        view = View.inflate(context, R.layout.count_item_shop_tranding, this);
        rvMore = view.findViewById(R.id.rv_more);
        tvTitle = view.findViewById(R.id.tv_title);
        llContent = view.findViewById(R.id.llContent);
        tvTimeSearch =view.findViewById(R.id.tv_time_search);
        recyclerView = view.findViewById(R.id.recyclerView);
        tv_text = view.findViewById(R.id.tv_text);
        tvTimeSearch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                SelectMonthDialog.Builder builder = new SelectMonthDialog.Builder(context);
                builder.setOnDialogClickListener((v, month) -> {
                    tv_text.setText(month+"商户交易量");
                    initDateData(month);
                });
                EasyDialog build = builder.build();
                build.setCancelable(true);
                build.show(fragmentManager);
            }
        });

        initRecycleView();
    }

    private void initDateData(String month) {
        countChartOtherClose.loadTrandShopCharData(month);
    }

    public void setData(List<MerchantTrading.MerchantTradingBean> datas ) {
        mAdapter.addData(datas);
    }


    public void setListener(CountChartActionClose listener) {
        countChartOtherClose = listener;
    }

    public void setTitle(String title) {
        if (tvTitle != null) tvTitle.setText(title);
    }

    public interface CountChartActionClose {

        void loadTrandShopCharData(String month);
    }


    private void initRecycleView() {
        //     设置布局控制器
        //  reverseLayout 是否实现反转
//        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        mAdapter = new MonthTrandAdapter(getContext(),datas);
        recyclerView.setAdapter(mAdapter);
        mAdapter.addData(datas);
    }


}
